<?php
class KeywordsAction extends CommonAction {
	private $account;
	private $password;
	private $cookie_jar;
	private $bid;

	public function index() {
		$this->display();
	}

	public function import()
	{
		$Import = M('Import');
		$keywords = $Import ->where(array('status'=>0))->select();
		$this->assign('keywords', $keywords);
		$this->display();
	}

	public function add()
	{
		$import_id = $this->_get('import_id');
		$Import = M('Import');
		$keywords = $Import ->where(array('status'=>0,'import_id'=>$import_id))->find();
		$keywords = unserialize($keywords['keywords']);
		foreach($keywords as $value)
		{
			$this->_addKeywords(iconv('utf-8', 'gb2312', $value));
		}
		$Import->where(array('import_id'=>$import_id))->save(array('status'=>1));
		$this->redirect('import');
	}

	private function _addKeywords ($keywords){
		$this->_init();
		$this->_login();
		$this->_add($keywords);
		$this->_delete();
	}
	
	private function _init(){
		$this->account 	= iconv('utf-8', 'gb2312', '2009年6月21号');
		$this->password 	= 'wodeyuan';
		$this->cookie_jar	= dirname($_SERVER["SCRIPT_FILENAME"]).'/cookie.txt';
	}
	
	private function _login(){
		$url				= 'https://passport.baidu.com/?login';
		$fields_post 	= 'tpl_ok=&next_target=&tpl=&skip_ok=&aid=&need_pay=&need_coin=&pay_method=';
		$fields_post	.='&u=./&return_method=get&more_param=&return_type=&psp_tt=0&password=';
		$fields_post 	.= $this->password;
		$fields_post 	.= '&safeflg=0&isphone=false&username='.$this->account.'&verifycode=';
		// 登录部份
		$curl_session = curl_init();
		curl_setopt($curl_session, CURLOPT_URL, $url);
		curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curl_session, CURLOPT_SSL_VERIFYHOST, 1);
		curl_setopt($curl_session, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
		curl_setopt($curl_session, CURLOPT_POST, 1);
		curl_setopt($curl_session, CURLOPT_POSTFIELDS,$fields_post);
		curl_setopt($curl_session, CURLOPT_COOKIEJAR, $this->cookie_jar);
		curl_setopt($curl_session, CURLOPT_COOKIEFILE, $this->cookie_jar);
		curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);
		$temp = curl_exec($curl_session);
		if ($temp == NULL) {
			echo "Error:
			";
			echo curl_errno($curl_session) . " - " . curl_error($curl_session) . "
			";
		}
		curl_close($curl_session);
	}

	private function _add($keywords){
		$url = 'http://index.baidu.com/main/myindex.php';
		$curl_session = curl_init();
		curl_setopt($curl_session, CURLOPT_URL, $url);
		curl_setopt($curl_session, CURLOPT_HEADER, 0);
		curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl_session, CURLOPT_COOKIEFILE, $this->cookie_jar);
		//curl_setopt($curl_session, CURLOPT_POST, 1);
		//curl_setopt($curl_session, CURLOPT_POSTFIELDS, $data);
		$temp = curl_exec($curl_session);
		curl_close($curl_session);
		//$pattern = '/bid=([\w]+)\"/';
		//preg_match_all($pattern, $temp, $matches);
		//$this->bid = $data['bid'] = $matches[1][0];
		$this->bid = $data['bid'] = 21781;
		$url="http://index.baidu.com/tools/add_word_to_board.php?u=http%3A%2F%2Findex.baidu.com%2Fmain%2Fmyboard.php%3Fbid%3D".$data['bid'];
		$data['word']=$keywords;
		$curl_session = curl_init();
		curl_setopt($curl_session, CURLOPT_URL, $url);
		curl_setopt($curl_session, CURLOPT_HEADER, 0);
		curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl_session, CURLOPT_COOKIEFILE, $this->cookie_jar);
		curl_setopt($curl_session, CURLOPT_POST, 1);
		curl_setopt($curl_session, CURLOPT_POSTFIELDS, $data);
		$temp = curl_exec($curl_session);
		curl_close($curl_session);
	}
	private function _delete(){
		$url = "http://index.baidu.com/main/myboard.php?bid=".$this->bid;
		$curl_session = curl_init();
		curl_setopt($curl_session, CURLOPT_URL, $url);
		curl_setopt($curl_session, CURLOPT_HEADER, 0);
		curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl_session, CURLOPT_COOKIEFILE, $this->cookie_jar);
		$temp = curl_exec($curl_session);
		curl_close($curl_session);
		
		$pattern='/tools\/delete_word_from_board\\.php\\?bwid=(.*?)&u=http/is';
		preg_match_all($pattern,$temp,$arr);
		
		$url='http://index.baidu.com/tools/delete_word_from_board.php?bwid='.$arr[1][3].'&u=http%3A%2F%2Findex.baidu.com%2Fmain%2Fmyboard.php%3Fbid%3D'.$this->bid;
		
		$curl_session = curl_init();
		curl_setopt($curl_session, CURLOPT_URL, $url);
		curl_setopt($curl_session, CURLOPT_HEADER, 0);
		curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl_session, CURLOPT_COOKIEFILE, $this->cookie_jar);
		$temp = curl_exec($curl_session);
		curl_close($curl_session);
	}
}